<template>
  <div class="profile-container">
    <div class="profile-header">
      <h1>个人信息</h1>
    </div>
    
    <div class="profile-content">
      <div class="profile-sidebar">
        <div class="avatar-section">
          <div class="avatar">
            <img :src="user.avatar || require('@/assets/logo.png')" alt="用户头像">
          </div>
          <input 
            type="file" 
            ref="avatarInput" 
            @change="handleAvatarChange" 
            accept="image/*" 
            style="display: none"
          />
          <button class="change-avatar-btn" @click="triggerAvatarUpload">更换头像</button>
        </div>
        
        <div class="sidebar-menu">
          <div class="menu-item" :class="{ active: selectedTab === 'basic' }" @click="changeTab('basic')">
            <i class="icon">👤</i>
            <span>基本信息</span>
          </div>
          <div class="menu-item" :class="{ active: selectedTab === 'notifications' }" @click="changeTab('notifications')">
            <i class="icon">🔔</i>
            <span>消息通知</span>
          </div>
          <div class="menu-item" :class="{ active: selectedTab === 'features' }" @click="changeTab('features')">
            <i class="icon">📚</i>
            <span>功能介绍</span>
          </div>
        </div>
      </div>
      
      <div class="profile-main">
        <div v-if="selectedTab === 'basic'" class="info-section">
          <h2>基本信息</h2>
          
          <!-- 加载状态显示 -->
          <div v-if="isLoading" class="loading-indicator">
            <div class="loading-spinner"></div>
            <p>加载中...</p>
          </div>
          
          <!-- 保存成功提示 -->
          <div v-if="saveSuccess" class="success-message">
            <i class="success-icon">✓</i>
            <span>保存成功</span>
          </div>
          
          <!-- 保存失败提示 -->
          <div v-if="saveError" class="error-message">
            <i class="error-icon">✗</i>
            <span>{{ errorMessage }}</span>
          </div>
          
          <form @submit.prevent="updateProfile" class="profile-form">
            <div class="form-row">
              <div class="form-group">
                <label class="form-label">用户名</label>
                <input type="text" v-model="user.name" class="form-input" placeholder="您的用户名" readonly>
              </div>
              
              <div class="form-group">
                <label class="form-label">邮箱</label>
                <input type="email" v-model="user.email" class="form-input" placeholder="您的邮箱地址" readonly>
              </div>
            </div>
            
            <div class="form-group">
              <label class="form-label">个人简介</label>
              <textarea v-model="user.bio" class="form-input form-textarea" 
                placeholder="简单介绍一下自己..."></textarea>
            </div>
            
            <div class="form-row">
              <div class="form-group">
                <label class="form-label">音乐身份</label>
                <select v-model="user.musicIdentity" class="form-input form-select">
                  <option value="hobbyist">音乐爱好者</option>
                  <option value="student">音乐学生</option>
                  <option value="teacher">音乐教师</option>
                  <option value="professional">专业音乐人</option>
                  <option value="other">其他</option>
                </select>
              </div>
              
              <div class="form-group">
                <label class="form-label">主要乐器</label>
                <select v-model="user.instrument" class="form-input form-select">
                  <option value="piano">钢琴</option>
                  <option value="guitar">吉他</option>
                  <option value="violin">小提琴</option>
                  <option value="drums">鼓</option>
                  <option value="vocal">声乐</option>
                  <option value="other">其他</option>
                  <option value="none">暂无</option>
                </select>
              </div>
            </div>
            
            <div class="form-group">
              <label class="form-label">音乐偏好</label>
              <div class="checkbox-group">
                <label class="checkbox-label">
                  <input type="checkbox" v-model="user.preferences" value="classical">
                  <span>古典音乐</span>
                </label>
                <label class="checkbox-label">
                  <input type="checkbox" v-model="user.preferences" value="jazz">
                  <span>爵士乐</span>
                </label>
                <label class="checkbox-label">
                  <input type="checkbox" v-model="user.preferences" value="pop">
                  <span>流行音乐</span>
                </label>
                <label class="checkbox-label">
                  <input type="checkbox" v-model="user.preferences" value="rock">
                  <span>摇滚乐</span>
                </label>
                <label class="checkbox-label">
                  <input type="checkbox" v-model="user.preferences" value="folk">
                  <span>民谣</span>
                </label>
                <label class="checkbox-label">
                  <input type="checkbox" v-model="user.preferences" value="electronic">
                  <span>电子音乐</span>
                </label>
              </div>
            </div>
            
            <div class="form-actions">
              <button type="submit" class="btn btn-primary">保存修改</button>
              <button type="button" class="btn btn-secondary" @click="cancelChanges">取消</button>
            </div>
          </form>
        </div>
        
        <div v-if="selectedTab === 'notifications'" class="notification-section">
          <h2>消息通知</h2>
          <div v-if="isLoading" class="loading-indicator">
            <div class="loading-spinner"></div>
            <p>加载中...</p>
          </div>
          
          <!-- 管理员状态提示 -->
          <div v-if="user.is_admin" class="admin-badge">
            <i class="admin-icon">👑</i>
            <span>管理员模式</span>
          </div>
          
          <!-- 管理员发布系统消息的表单 -->
          <div v-if="user.is_admin" class="admin-notification-form">
            <h3>发布系统通知</h3>
            <form @submit.prevent="createSystemNotification" class="notification-form">
              <div class="form-group">
                <label class="form-label">通知标题</label>
                <input type="text" v-model="notificationForm.title" class="form-input" placeholder="输入通知标题..." required>
              </div>
              
              <div class="form-group">
                <label class="form-label">通知内容</label>
                <textarea v-model="notificationForm.content" class="form-input form-textarea" placeholder="输入通知内容..." required></textarea>
              </div>
              
              <div class="form-group">
                <label class="form-label">通知类型</label>
                <select v-model="notificationForm.type" class="form-input form-select">
                  <option value="system">系统通知</option>
                  <option value="update">更新公告</option>
                  <option value="maintenance">维护通知</option>
                </select>
              </div>
              
              <div class="form-actions">
                <button type="submit" class="btn btn-primary">发布通知</button>
                <button type="button" class="btn btn-secondary" @click="resetNotificationForm">重置</button>
              </div>
            </form>
          </div>
          
          <!-- 系统消息列表 -->
          <div class="notification-list-container">
            <h3>系统通知</h3>
            
            <div v-if="notifications.length === 0 && !isLoading" class="notification-empty">
              <i class="empty-icon">📬</i>
              <p>暂无消息通知</p>
              <p class="empty-desc">我们会将系统公告和重要更新发送到这里</p>
            </div>
            
            <div v-else class="notification-list">
              <div v-for="notification in notifications" :key="notification.id" class="notification-item">
                <div class="notification-header">
                  <span class="notification-type" :class="notification.type">{{ getNotificationTypeText(notification.type) }}</span>
                  <span class="notification-date">{{ formatDate(notification.created_at) }}</span>
                </div>
                
                <h4 class="notification-title">{{ notification.title }}</h4>
                <div class="notification-content">{{ notification.content }}</div>
                
                <!-- 管理员编辑和删除按钮 -->
                <div v-if="user.is_admin" class="notification-admin-actions">
                  <button class="btn btn-small btn-outline" @click="editNotification(notification)">编辑</button>
                  <button class="btn btn-small btn-danger" @click="deleteNotification(notification.id)">删除</button>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 管理员编辑通知的模态框 -->
          <div v-if="showEditModal" class="modal-overlay" @click.self="showEditModal = false">
            <div class="modal-container">
              <div class="modal-header">
                <h3>编辑系统通知</h3>
                <button class="modal-close" @click="showEditModal = false">&times;</button>
              </div>
              
              <div class="modal-body">
                <form @submit.prevent="updateNotification" class="notification-form">
                  <div class="form-group">
                    <label class="form-label">通知标题</label>
                    <input type="text" v-model="editingNotification.title" class="form-input" required>
                  </div>
                  
                  <div class="form-group">
                    <label class="form-label">通知内容</label>
                    <textarea v-model="editingNotification.content" class="form-input form-textarea" required></textarea>
                  </div>
                  
                  <div class="form-group">
                    <label class="form-label">通知类型</label>
                    <select v-model="editingNotification.type" class="form-input form-select">
                      <option value="system">系统通知</option>
                      <option value="update">更新公告</option>
                      <option value="maintenance">维护通知</option>
                    </select>
                  </div>
                  
                  <div class="form-actions">
                    <button type="submit" class="btn btn-primary">保存修改</button>
                    <button type="button" class="btn btn-secondary" @click="showEditModal = false">取消</button>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
        
        <div v-if="selectedTab === 'features'" class="features-section">
          <h2>功能介绍</h2>
          
          <div class="feature-redirect-container">
            <p class="feature-redirect-message">点击下方按钮访问我们的功能介绍网站，了解更多关于音乐教育平台的详细功能。</p>
            <button class="feature-redirect-btn" @click="redirectToFeatureSite">
              <i class="feature-redirect-icon">🔗</i>
              访问功能介绍网站
            </button>
          </div>
        </div>
        
        <div v-if="selectedTab === 'basic'" class="stats-section">
          <h2>音乐活动统计</h2>
          <div class="stats-grid">
            <div class="stat-card">
              <div class="stat-value">{{ formatNumber(user.stats.scanned) }}</div>
              <div class="stat-label">乐谱识别</div>
            </div>
            <div class="stat-card">
              <div class="stat-value">{{ formatNumber(user.stats.conversations) }}</div>
              <div class="stat-label">助手对话</div>
            </div>
            <div class="stat-card">
              <div class="stat-value">{{ formatNumber(user.stats.posts) }}</div>
              <div class="stat-label">社区发帖</div>
            </div>
            <div class="stat-card">
              <div class="stat-value">{{ formatNumber(user.stats.comments) }}</div>
              <div class="stat-label">社区评论</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { API_ENDPOINTS } from '../config';

export default {
  name: 'ProfileView',
  data() {
    return {
      user: {
        name: '',
        email: '',
        bio: '',
        avatar: '',
        musicIdentity: 'hobbyist',
        instrument: 'none',
        preferences: [],
        stats: {
          scanned: 0,
          conversations: 0,
          posts: 0,
          comments: 0,
          likes_received: 0,
          likes_given: 0,
          followers: 0,
          following: 0
        }
      },
      originalUser: null, // 用于存储原始数据，便于检测变更
      isLoading: true,
      saveSuccess: false,
      saveError: false,
      errorMessage: '',
      avatarFile: null, // 存储准备上传的头像文件
      uploadProgress: 0, // 上传进度
      selectedTab: 'basic', // 当前选中的侧边栏选项
      activeMenu: 'profile', // 当前活动菜单
      notificationForm: {
        title: '',
        content: '',
        type: 'system'
      },
      notifications: [],
      showEditModal: false,
      editingNotification: null
    }
  },
  mounted() {
    // 加载用户个人资料
    this.fetchUserProfile();
    
    // 加载系统通知
    this.fetchSystemNotifications();
  },
  methods: {
    async fetchUserProfile() {
      this.isLoading = true;
      
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          this.saveError = true;
          this.errorMessage = '您尚未登录，请先登录';
          this.isLoading = false;
          return;
        }
        
        // 发送请求到后端API
        const response = await axios.get(API_ENDPOINTS.PROFILE_GET, {
          headers: {
            'Authorization': `Bearer ${token}`
          }
        });
        
        // 更新用户数据
        const userData = response.data;
        this.user = {
          id: userData.id,
          name: userData.username || '',
          email: userData.email || '',
          bio: userData.bio || '',
          avatar: userData.avatar_url || '',
          musicIdentity: userData.music_identity || 'hobbyist',
          instrument: userData.main_instrument || 'none',
          preferences: userData.preferences || [],
          stats: userData.stats || {
            scanned: 0,
            conversations: 0,
            posts: 0,
            comments: 0,
            likes_received: 0,
            likes_given: 0,
            followers: 0,
            following: 0
          },
          is_admin: userData.is_admin || false
        };
        
        // 保存原始数据副本
        this.originalUser = JSON.parse(JSON.stringify(this.user));
        
        // 打印用户管理员状态，用于调试
        console.log('用户管理员状态:', this.user.is_admin);
        
        this.isLoading = false;
      } catch (error) {
        console.error('获取用户资料失败:', error);
        this.saveError = true;
        this.errorMessage = error.response?.data?.detail || '获取用户资料失败，请重试';
        this.isLoading = false;
      }
    },
    
    async updateProfile() {
      this.isLoading = true;
      this.saveSuccess = false;
      this.saveError = false;
      
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          this.saveError = true;
          this.errorMessage = '您尚未登录，请先登录';
          this.isLoading = false;
          return;
        }
        
        // 准备更新数据 - 不包含用户名和邮箱
        const updateData = {
          full_name: this.user.name,
          bio: this.user.bio,
          music_identity: this.user.musicIdentity,
          main_instrument: this.user.instrument,
          preferences: this.user.preferences
        };
        
        // 发送请求到后端API
        const response = await axios.put(API_ENDPOINTS.PROFILE_UPDATE, updateData, {
          headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        });
        
        // 更新头像URL
        this.user.avatar = response.data.avatar_url;
        this.originalUser.avatar = response.data.avatar_url;
        
        // 更新导航栏的头像
        this.updateNavbarAvatar(response.data.avatar_url);
        
        // 更新成功
        this.saveSuccess = true;
        
        // 更新本地用户数据
        const userData = response.data;
        this.user = {
          id: userData.id,
          name: userData.username || '',
          email: userData.email || '',
          bio: userData.bio || '',
          avatar: userData.avatar_url || '',
          musicIdentity: userData.music_identity || 'hobbyist',
          instrument: userData.main_instrument || 'none',
          preferences: userData.preferences || [],
          stats: userData.stats || this.user.stats,
          is_admin: userData.is_admin || false
        };
        
        // 更新原始数据副本
        this.originalUser = JSON.parse(JSON.stringify(this.user));
        
        // 3秒后隐藏成功消息并刷新页面
        setTimeout(() => {
          this.saveSuccess = false;
          // 刷新页面以确保所有组件都更新头像
          window.location.reload();
        }, 3000);
      } catch (error) {
        console.error('更新用户资料失败:', error);
        this.saveError = true;
        this.errorMessage = error.response?.data?.detail || '更新用户资料失败，请重试';
      } finally {
        this.isLoading = false;
      }
    },
    
    triggerAvatarUpload() {
      // 触发文件选择对话框
      this.$refs.avatarInput.click();
    },
    
    handleAvatarChange(event) {
      const file = event.target.files[0];
      if (file) {
        // 预览所选文件
        this.avatarFile = file;
        const reader = new FileReader();
        reader.onload = (e) => {
          this.user.avatar = e.target.result;
        };
        reader.readAsDataURL(file);
        
        // 自动上传
        this.uploadAvatar();
      }
    },
    
    async uploadAvatar() {
      if (!this.avatarFile) return;
      
      this.isLoading = true;
      this.uploadProgress = 0;
      
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          this.saveError = true;
          this.errorMessage = '您尚未登录，请先登录';
          this.isLoading = false;
          return;
        }
        
        // 准备表单数据
        const formData = new FormData();
        formData.append('avatar', this.avatarFile);
        
        // 发送请求到后端API
        const response = await axios.post(API_ENDPOINTS.PROFILE_AVATAR, formData, {
          headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'multipart/form-data'
          },
          onUploadProgress: (progressEvent) => {
            this.uploadProgress = Math.round((progressEvent.loaded * 100) / progressEvent.total);
          }
        });
        
        // 更新头像URL
        this.user.avatar = response.data.avatar_url;
        this.originalUser.avatar = response.data.avatar_url;
        
        // 更新导航栏的头像
        this.updateNavbarAvatar(response.data.avatar_url);
        
        // 更新成功
        this.saveSuccess = true;
        
        // 3秒后隐藏成功消息并刷新页面
        setTimeout(() => {
          this.saveSuccess = false;
          // 刷新页面以确保所有组件都更新头像
          window.location.reload();
        }, 3000);
      } catch (error) {
        console.error('上传头像失败:', error);
        this.saveError = true;
        this.errorMessage = error.response?.data?.detail || '上传头像失败，请重试';
      } finally {
        this.isLoading = false;
        this.avatarFile = null;
        this.uploadProgress = 0;
      }
    },
    
    // 切换侧边栏选项
    changeTab(tab) {
      this.selectedTab = tab;
    },
    
    // 取消更改
    cancelChanges() {
      // 恢复到原始数据
      if (this.originalUser) {
        this.user = JSON.parse(JSON.stringify(this.originalUser));
      }
    },
    
    // 格式化数字
    formatNumber(number) {
      if (number >= 1000) {
        return (number / 1000).toFixed(1) + 'k';
      }
      return number;
    },
    
    // 更新导航栏的头像
    updateNavbarAvatar(avatarUrl) {
      // 更新本地存储中的用户信息，确保导航栏能获取最新头像
      try {
        // 从本地存储获取用户信息
        const userStr = localStorage.getItem('user');
        if (userStr) {
          const user = JSON.parse(userStr);
          // 更新头像URL
          user.avatar_url = avatarUrl;
          user.avatar = avatarUrl;
          // 保存回本地存储
          localStorage.setItem('user', JSON.stringify(user));
          
          // 触发用户信息刷新事件，通知App.vue更新导航栏
          window.dispatchEvent(new CustomEvent('user-avatar-updated', { detail: { avatarUrl } }));
        }
      } catch (error) {
        console.error('更新导航栏头像失败:', error);
      }
    },
    
    async createSystemNotification() {
      this.isLoading = true;
      
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          this.saveError = true;
          this.errorMessage = '您尚未登录，请先登录';
          this.isLoading = false;
          return;
        }
        
        // 发送请求到后端API
        const response = await axios.post(API_ENDPOINTS.SYSTEM_NOTIFICATION_CREATE, this.notificationForm, {
          headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        });
        
        // 更新系统通知列表
        this.notifications.push(response.data);
        
        // 重置表单
        this.notificationForm = {
          title: '',
          content: '',
          type: 'system'
        };
        
        // 更新成功
        this.saveSuccess = true;
        
        // 3秒后隐藏成功消息并刷新页面
        setTimeout(() => {
          this.saveSuccess = false;
          // 刷新页面以确保所有组件都更新通知列表
          this.fetchUserProfile();
        }, 3000);
      } catch (error) {
        console.error('发布系统通知失败:', error);
        this.saveError = true;
        this.errorMessage = error.response?.data?.detail || '发布系统通知失败，请重试';
      } finally {
        this.isLoading = false;
      }
    },
    
    async editNotification(notification) {
      this.editingNotification = {
        id: notification.id,
        title: notification.title,
        content: notification.content,
        type: notification.type
      };
      this.showEditModal = true;
    },
    
    async updateNotification() {
      this.isLoading = true;
      
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          this.saveError = true;
          this.errorMessage = '您尚未登录，请先登录';
          this.isLoading = false;
          return;
        }
        
        // 检查ID是否存在
        if (!this.editingNotification.id) {
          this.saveError = true;
          this.errorMessage = '通知ID无效，无法更新';
          this.isLoading = false;
          return;
        }
        
        // 发送请求到后端API
        const response = await axios.put(
          API_ENDPOINTS.SYSTEM_NOTIFICATION_UPDATE(this.editingNotification.id), 
          this.editingNotification, 
          {
            headers: {
              'Authorization': `Bearer ${token}`,
              'Content-Type': 'application/json'
            }
          }
        );
        
        // 更新系统通知列表
        const index = this.notifications.findIndex(n => n.id === this.editingNotification.id);
        if (index !== -1) {
          this.notifications[index] = response.data;
        }
        
        // 重置编辑模态框
        this.editingNotification = null;
        this.showEditModal = false;
        
        // 更新成功
        this.saveSuccess = true;
        
        // 3秒后隐藏成功消息并刷新页面
        setTimeout(() => {
          this.saveSuccess = false;
          // 刷新页面以确保所有组件都更新通知列表
          this.fetchSystemNotifications();
        }, 3000);
      } catch (error) {
        console.error('编辑系统通知失败:', error);
        this.saveError = true;
        this.errorMessage = error.response?.data?.detail || '编辑系统通知失败，请重试';
      } finally {
        this.isLoading = false;
      }
    },
    
    async deleteNotification(id) {
      this.isLoading = true;
      
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          this.saveError = true;
          this.errorMessage = '您尚未登录，请先登录';
          this.isLoading = false;
          return;
        }
        
        // 发送请求到后端API
        await axios.delete(API_ENDPOINTS.SYSTEM_NOTIFICATION_DELETE(id), {
          headers: {
            'Authorization': `Bearer ${token}`
          }
        });
        
        // 更新系统通知列表
        this.notifications = this.notifications.filter(n => n.id !== id);
        
        // 更新成功
        this.saveSuccess = true;
        
        // 3秒后隐藏成功消息并刷新页面
        setTimeout(() => {
          this.saveSuccess = false;
          // 刷新页面以确保所有组件都更新通知列表
          this.fetchUserProfile();
        }, 3000);
      } catch (error) {
        console.error('删除系统通知失败:', error);
        this.saveError = true;
        this.errorMessage = error.response?.data?.detail || '删除系统通知失败，请重试';
      } finally {
        this.isLoading = false;
      }
    },
    
    redirectToFeatureSite() {
      // 实现跳转到功能介绍网站的逻辑
      console.log('跳转到功能介绍网站');
    },
    
    getNotificationTypeText(type) {
      switch (type) {
        case 'system':
          return '系统通知';
        case 'update':
          return '更新公告';
        case 'maintenance':
          return '维护通知';
        default:
          return '未知类型';
      }
    },
    
    formatDate(dateStr) {
      const date = new Date(dateStr);
      return date.toLocaleDateString();
    },
    
    async fetchSystemNotifications() {
      try {
        // 从本地存储获取token
        const token = localStorage.getItem('token');
        if (!token) {
          console.error('获取系统通知失败: 未登录');
          return;
        }
        
        // 发送请求到后端API
        const response = await axios.get(API_ENDPOINTS.SYSTEM_NOTIFICATIONS, {
          headers: {
            'Authorization': `Bearer ${token}`
          }
        });
        
        // 更新通知列表
        this.notifications = response.data || [];
      } catch (error) {
        console.error('获取系统通知失败:', error);
      }
    },
    
    resetNotificationForm() {
      this.notificationForm = {
        title: '',
        content: '',
        type: 'system'
      };
    }
  }
}
</script>

<style scoped>
.profile-container {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.dark-theme .profile-container {
  background-color: #1a1a1a;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.profile-header {
  text-align: center;
  margin-bottom: 30px;
}

.dark-theme .profile-header h2 {
  color: #e0e0e0;
}

.profile-avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: 0 auto 20px;
  border: 4px solid #e0e7ff;
}

.dark-theme .profile-avatar {
  border-color: #3a3a45;
}

.profile-info {
  margin-bottom: 30px;
}

.dark-theme .profile-info h3 {
  color: #e0e0e0;
}

.dark-theme .profile-info p {
  color: #aaaaaa;
}

.info-item {
  margin-bottom: 15px;
  padding: 15px;
  background-color: #f9fafb;
  border-radius: 8px;
}

.dark-theme .info-item {
  background-color: #252530;
}

.dark-theme .info-item label {
  color: #e0e0e0;
}

.dark-theme .info-item input {
  background-color: #1a1a1a;
  border-color: #3a3a45;
  color: #e0e0e0;
}

.dark-theme .info-item input:focus {
  border-color: #818cf8;
  box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.2);
}

.profile-actions {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 30px;
}

.dark-theme .btn-primary {
  background-color: #818cf8;
  color: #ffffff;
}

.dark-theme .btn-primary:hover {
  background-color: #717cf8;
}

.dark-theme .btn-secondary {
  background-color: #2a2a35;
  color: #e0e0e0;
}

.dark-theme .btn-secondary:hover {
  background-color: #333340;
}

.profile-content {
  display: flex;
  gap: 30px;
  min-height: 600px;
}

.profile-sidebar {
  width: 280px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 5px 20px rgba(67, 97, 238, 0.08);
  overflow: hidden;
}

.dark-theme .profile-sidebar {
  background: #1a1a1a;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
}

.avatar-section {
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-bottom: 1px solid rgba(67, 97, 238, 0.08);
}

.dark-theme .avatar-section {
  border-bottom-color: #3a3a45;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 20px;
  border: 3px solid #e0e7ff;
  box-shadow: 0 5px 15px rgba(67, 97, 238, 0.15);
}

.dark-theme .avatar {
  border-color: #3a3a45;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.change-avatar-btn {
  background: none;
  border: 1px solid #e0e7ff;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  color: #4361ee;
  cursor: pointer;
  transition: all 0.3s;
}

.dark-theme .change-avatar-btn {
  border-color: #3a3a45;
  color: #e0e0e0;
}

.dark-theme .change-avatar-btn:hover {
  background-color: #252530;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.sidebar-menu {
  padding: 20px 0;
}

.menu-item {
  padding: 12px 24px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s;
  color: #555;
}

.dark-theme .menu-item {
  color: #aaaaaa;
}

.menu-item:hover {
  background-color: rgba(67, 97, 238, 0.05);
  color: #4361ee;
}

.dark-theme .menu-item:hover {
  background-color: #252530;
  color: #e0e0e0;
}

.menu-item.active {
  background-color: rgba(67, 97, 238, 0.1);
  color: #4361ee;
  font-weight: 500;
  border-left: 3px solid #4361ee;
}

.dark-theme .menu-item.active {
  background-color: #252530;
  color: #818cf8;
  border-left-color: #818cf8;
}

.icon {
  margin-right: 12px;
  font-size: 18px;
}

.profile-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.info-section, .stats-section {
  background: white;
  border-radius: 16px;
  box-shadow: 0 5px 20px rgba(67, 97, 238, 0.08);
  padding: 30px;
}

.dark-theme .info-section, 
.dark-theme .stats-section {
  background: #1a1a1a;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
}

.info-section h2, .stats-section h2 {
  font-size: 20px;
  font-weight: 600;
  margin-top: 0;
  margin-bottom: 25px;
  color: #333;
}

.dark-theme .info-section h2, 
.dark-theme .stats-section h2 {
  color: #e0e0e0;
}

.profile-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-row {
  display: flex;
  gap: 20px;
}

.form-row .form-group {
  flex: 1;
}

.form-label {
  display: block;
  font-weight: 500;
  margin-bottom: 8px;
  color: #555;
}

.dark-theme .form-label {
  color: #e0e0e0;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e0e7ff;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.3s;
}

.dark-theme .form-input {
  background-color: #252530;
  border-color: #3a3a45;
  color: #e0e0e0;
}

.form-input:focus {
  outline: none;
  border-color: #4361ee;
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.1);
}

.form-textarea {
  min-height: 100px;
  resize: vertical;
}

.form-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23555' d='M6 8.825l-4.293-4.293a1 1 0 0 1 1.414-1.414L6 5.997l2.879-2.879a1 1 0 0 1 1.414 1.414L6 8.825z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 15px center;
  padding-right: 40px;
}

.dark-theme .form-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23e0e0e0' d='M6 8.825l-4.293-4.293a1 1 0 0 1 1.414-1.414L6 5.997l2.879-2.879a1 1 0 0 1 1.414 1.414L6 8.825z'/%3E%3C/svg%3E");
}

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 10px;
}

.checkbox-label {
  position: relative;
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  border-radius: 20px;
  background-color: #f3f4f6;
  border: 1px solid #e0e7ff;
  cursor: pointer;
  transition: all 0.3s ease;
  user-select: none;
}

.checkbox-label input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkbox-label span {
  font-size: 14px;
  color: #4b5563;
  transition: all 0.3s ease;
}

.checkbox-label:hover {
  background-color: #e0e7ff;
  border-color: #818cf8;
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(129, 140, 248, 0.2);
}

.checkbox-label input:checked + span {
  color: #818cf8;
  font-weight: 500;
}

.checkbox-label input:checked ~ .checkbox-label {
  background-color: #e0e7ff;
  border-color: #818cf8;
}

.dark-theme .checkbox-label {
  background-color: #252530;
  border-color: #3a3a45;
}

.dark-theme .checkbox-label span {
  color: #e0e0e0;
}

.dark-theme .checkbox-label:hover {
  background-color: #333340;
  border-color: #818cf8;
  box-shadow: 0 2px 8px rgba(129, 140, 248, 0.3);
}

.dark-theme .checkbox-label input:checked + span {
  color: #818cf8;
}

.dark-theme .checkbox-label input:checked ~ .checkbox-label {
  background-color: #333340;
  border-color: #818cf8;
}

.form-actions {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.form-actions .btn {
  padding: 12px 25px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.stat-card {
  background: linear-gradient(135deg, rgba(67, 97, 238, 0.05), rgba(58, 12, 163, 0.05));
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  transition: all 0.3s;
  border: 1px solid rgba(67, 97, 238, 0.08);
}

.dark-theme .stat-card {
  background: linear-gradient(135deg, rgba(129, 140, 248, 0.05), rgba(99, 102, 241, 0.05));
  border-color: #3a3a45;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(67, 97, 238, 0.1);
  background: linear-gradient(135deg, rgba(67, 97, 238, 0.08), rgba(58, 12, 163, 0.08));
}

.dark-theme .stat-card:hover {
  background: linear-gradient(135deg, rgba(129, 140, 248, 0.08), rgba(99, 102, 241, 0.08));
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}

.stat-value {
  font-size: 32px;
  font-weight: 700;
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  margin-bottom: 8px;
}

.dark-theme .stat-value {
  background: linear-gradient(135deg, #818cf8, #6366f1);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

.stat-label {
  color: #666;
  font-size: 14px;
}

.dark-theme .stat-label {
  color: #aaaaaa;
}

/* 响应式样式 */
@media (max-width: 992px) {
  .profile-content {
    flex-direction: column;
  }
  
  .profile-sidebar {
    width: 100%;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .form-row {
    flex-direction: column;
    gap: 15px;
  }
  
  .avatar {
    width: 100px;
    height: 100px;
  }
}

@media (max-width: 576px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
}

/* 加载状态 */
.loading-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 20px 0;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.loading-spinner {
  width: 30px;
  height: 30px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #6366f1;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 成功消息 */
.success-message {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  background-color: #d1fae5;
  color: #065f46;
  border-radius: 8px;
  margin-bottom: 20px;
}

.success-icon {
  font-size: 18px;
  margin-right: 10px;
  background: #10b981;
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 错误消息 */
.error-message {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  background-color: #fee2e2;
  color: #991b1b;
  border-radius: 8px;
  margin-bottom: 20px;
}

.error-icon {
  font-size: 18px;
  margin-right: 10px;
  background: #ef4444;
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 头像上传样式 */
.avatar-section {
  position: relative;
  margin-bottom: 30px;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: 0 auto 15px;
  overflow: hidden;
  border: 4px solid #e0e7ff;
  position: relative;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.change-avatar-btn {
  display: block;
  background: #4f46e5;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 20px;
  font-size: 14px;
  margin: 0 auto;
  cursor: pointer;
  transition: background 0.3s;
}

.change-avatar-btn:hover {
  background: #4338ca;
}

/* 暗色模式适配 */
.dark-theme .loading-indicator {
  background-color: #1a1a1a;
}

.dark-theme .loading-spinner {
  border: 3px solid #333;
  border-top: 3px solid #818cf8;
}

.dark-theme .success-message {
  background-color: #064e3b;
  color: #a7f3d0;
}

.dark-theme .error-message {
  background-color: #7f1d1d;
  color: #fecaca;
}

/* 消息通知样式 */
.notification-section {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

.dark-theme .notification-section {
  background-color: #1a1a1a;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.notification-list-container {
  min-height: 300px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.notification-empty {
  text-align: center;
  padding: 40px 0;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 10px;
  color: #aaaaaa;
}

.empty-desc {
  color: #999999;
  font-size: 0.9rem;
  margin-top: 5px;
}

.notification-item {
  background-color: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 15px;
  transition: all 0.3s ease;
  border-left: 4px solid #4361ee;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.notification-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.notification-item.system {
  border-left-color: #4361ee;
}

.notification-item.update {
  border-left-color: #10b981;
}

.notification-item.maintenance {
  border-left-color: #f59e0b;
}

.dark-theme .notification-item {
  background-color: #252530;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.notification-type {
  padding: 5px 15px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
}

.notification-type.system {
  background-color: #e0e7ff;
  color: #4361ee;
}

.notification-type.update {
  background-color: #d1fae5;
  color: #10b981;
}

.notification-type.maintenance {
  background-color: #fef3c7;
  color: #f59e0b;
}

.notification-type::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.notification-type.system::before {
  background-color: #4361ee;
}

.notification-type.update::before {
  background-color: #10b981;
}

.notification-type.maintenance::before {
  background-color: #f59e0b;
}

.notification-title {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 12px 0;
  color: #333333;
}

.dark-theme .notification-title {
  color: #e0e0e0;
}

.notification-content {
  font-size: 0.95rem;
  line-height: 1.6;
  color: #555555;
  margin-bottom: 15px;
  white-space: pre-line;
}

.dark-theme .notification-content {
  color: #aaaaaa;
}

.notification-admin-actions {
  display: flex;
  gap: 12px;
  margin-top: 15px;
  justify-content: flex-end;
}

.btn-small {
  padding: 8px 16px;
  font-size: 0.85rem;
  border-radius: 20px;
  transition: all 0.2s;
}

.btn-outline {
  background: none;
  border: 1px solid #4361ee;
  color: #4361ee;
}

.btn-outline:hover {
  background-color: #4361ee;
  color: white;
  box-shadow: 0 2px 8px rgba(67, 97, 238, 0.3);
}

.btn-danger {
  background: none;
  border: 1px solid #ef4444;
  color: #ef4444;
  cursor: pointer;
}

.btn-danger:hover {
  background-color: #ef4444;
  color: white;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(3px);
}

.modal-container {
  background-color: white;
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.dark-theme .modal-container {
  background-color: #1a1a1a;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 25px;
  border-bottom: 1px solid #e0e7ff;
}

.dark-theme .modal-header {
  border-bottom-color: #3a3a45;
}

.modal-header h3 {
  margin: 0;
  color: #333;
  font-size: 1.3rem;
}

.dark-theme .modal-header h3 {
  color: #e0e0e0;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #999;
  cursor: pointer;
  transition: color 0.2s;
}

.modal-close:hover {
  color: #ef4444;
}

.modal-body {
  padding: 25px;
}

/* 添加管理员徽章样式 */
.admin-badge {
  display: inline-flex;
  align-items: center;
  background-color: #fef3c7;
  color: #92400e;
  border-radius: 30px;
  padding: 7px 20px;
  margin-bottom: 25px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(146, 64, 14, 0.1);
}

.admin-icon {
  margin-right: 10px;
  font-size: 1.2rem;
}

.admin-notification-form {
  background-color: #f0f9ff;
  border: 1px solid #c2e0ff;
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 30px;
  box-shadow: 0 3px 10px rgba(0, 120, 255, 0.1);
  transition: all 0.3s ease;
}

.admin-notification-form:hover {
  box-shadow: 0 5px 15px rgba(0, 120, 255, 0.15);
  transform: translateY(-2px);
}

.dark-theme .admin-notification-form {
  background-color: #0d2137;
  border-color: #1a4062;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

.admin-badge {
  display: inline-flex;
  align-items: center;
  background-color: #fef3c7;
  color: #92400e;
  border-radius: 30px;
  padding: 7px 20px;
  margin-bottom: 25px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(146, 64, 14, 0.1);
}

.admin-icon {
  margin-right: 10px;
  font-size: 1.2rem;
}

.notification-item {
  background-color: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 15px;
  transition: all 0.3s ease;
  border-left: 4px solid #4361ee;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.notification-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.notification-item.system {
  border-left-color: #4361ee;
}

.notification-item.update {
  border-left-color: #10b981;
}

.notification-item.maintenance {
  border-left-color: #f59e0b;
}

.dark-theme .notification-item {
  background-color: #252530;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.notification-type {
  padding: 5px 15px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
}

.notification-type.system {
  background-color: #e0e7ff;
  color: #4361ee;
}

.notification-type.update {
  background-color: #d1fae5;
  color: #10b981;
}

.notification-type.maintenance {
  background-color: #fef3c7;
  color: #f59e0b;
}

.notification-type::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.notification-type.system::before {
  background-color: #4361ee;
}

.notification-type.update::before {
  background-color: #10b981;
}

.notification-type.maintenance::before {
  background-color: #f59e0b;
}

.notification-title {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 12px 0;
  color: #333333;
}

.dark-theme .notification-title {
  color: #e0e0e0;
}

.notification-content {
  font-size: 0.95rem;
  line-height: 1.6;
  color: #555555;
  margin-bottom: 15px;
  white-space: pre-line;
}

.dark-theme .notification-content {
  color: #aaaaaa;
}

.notification-admin-actions {
  display: flex;
  gap: 12px;
  margin-top: 15px;
  justify-content: flex-end;
}

.btn-small {
  padding: 8px 16px;
  font-size: 0.85rem;
  border-radius: 20px;
  transition: all 0.2s;
}

.btn-outline {
  background: none;
  border: 1px solid #4361ee;
  color: #4361ee;
}

.btn-outline:hover {
  background-color: #4361ee;
  color: white;
  box-shadow: 0 2px 8px rgba(67, 97, 238, 0.3);
}

.btn-danger {
  background: none;
  border: 1px solid #ef4444;
  color: #ef4444;
  cursor: pointer;
}

.btn-danger:hover {
  background-color: #ef4444;
  color: white;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(3px);
}

.modal-container {
  background-color: white;
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.dark-theme .modal-container {
  background-color: #1a1a1a;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 25px;
  border-bottom: 1px solid #e0e7ff;
}

.dark-theme .modal-header {
  border-bottom-color: #3a3a45;
}

.modal-header h3 {
  margin: 0;
  color: #333;
  font-size: 1.3rem;
}

.dark-theme .modal-header h3 {
  color: #e0e0e0;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #999;
  cursor: pointer;
  transition: color 0.2s;
}

.modal-close:hover {
  color: #ef4444;
}

.modal-body {
  padding: 25px;
}
</style> 